package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.minio.service.FileUploadServer;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.wemedia.dtos.WmMaterialDto;
import com.heima.model.common.wemedia.pojo.WmMaterial;
import com.heima.model.common.wemedia.pojo.WmUser;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.heima.common.exception.com.heima.common.util.UserThreadLocal.USER_THREAD_LOCAL;


@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileUploadServer fileUploadServer;
    @Override
    public PageResponseResult getMaterialList(WmMaterialDto dto) {

        WmUser wmUser = (WmUser)USER_THREAD_LOCAL.get();
        Integer userId = wmUser.getId();
        log.error("获取到用户id为{}",wmUser.getId());
        if (userId==null){
            PageResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        dto.checkParam();
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        IPage<WmMaterial> pageData = new Page<>(page, size);
        //判断是否展示全部
        Short isCollection = dto.getIsCollection();
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(isCollection==(short)1,WmMaterial::getIsCollection,1);
        //TODO: 获取当前用户的id
        queryWrapper.eq(WmMaterial::getUserId,userId);
        this.page(pageData,queryWrapper);
        //获取返回需要的数据
        long total = pageData.getTotal();
        List<WmMaterial> wmMaterials = pageData.getRecords();
        PageResponseResult responseResult = new PageResponseResult(page,size,(int)total);
        responseResult.ok(wmMaterials);
        return responseResult;
    }

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //验证文件是否存在
        if (multipartFile!=null&&multipartFile.getSize()>0) {
            //获取原文件名
            String originalFilename = multipartFile.getOriginalFilename();
            //上传到minio中
            try {
                //文件在minio中的地址
                String uploadFilePath = fileUploadServer.uploadFile(
                        originalFilename,
                        multipartFile.getContentType(),
                        multipartFile.getInputStream());
                //添加数据
                WmUser wmUser = (WmUser)USER_THREAD_LOCAL.get();
                WmMaterial wmMaterial = new WmMaterial();
                wmMaterial.setUserId(wmUser.getId());
                wmMaterial.setUrl(uploadFilePath);
                wmMaterial.setIsCollection((short)0);
                wmMaterial.setType((short)0);
                wmMaterial.setCreatedTime(new Date());
                this.save(wmMaterial);
                return ResponseResult.okResult(wmMaterial);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    @Override
    public ResponseResult collect(Integer id) {
        WmMaterial wmMaterial = this.getById(id);
        wmMaterial.setIsCollection((short) 1);
        this.updateById(wmMaterial);
        return ResponseResult.okResult("收藏");
    }

    @Override
    public ResponseResult cancelCollect(Integer id) {
        WmMaterial wmMaterial = this.getById(id);
        wmMaterial.setIsCollection((short) 0);
        this.updateById(wmMaterial);
        return ResponseResult.okResult("取消收藏");
    }
}